package com.sgb.kjwl.view.ui.adapter.search;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemWebsiteMaterialGoodsBinding;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author Administrator
 */
public class WebGoodsMaterialAdapter extends BaseRecycleAdapter<GoodsListEntity> {

    DecimalFormat df = new DecimalFormat("#.00");

    public WebGoodsMaterialAdapter(Context context, List<GoodsListEntity> data) {
        super(context, R.layout.item_website_material_goods, data);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, final GoodsListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemWebsiteMaterialGoodsBinding itemMaterialGoodsBinding = (ItemWebsiteMaterialGoodsBinding) holder.getmBinding();
        itemMaterialGoodsBinding.contentView.setOnClickListener(v -> {
            if (TextUtils.isEmpty(entity.getCompanyNo())) {
                ToastUtil.show(holder.itemView.getContext(), "企业店铺No不能为空");
                return;
            }
            WebsiteInfoActivity.start(holder.itemView.getContext(), entity.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
        });
        if ("1".equals(entity.getSelfSupportTag())) {
            itemMaterialGoodsBinding.ziyingIcon.setVisibility(View.VISIBLE);
        } else {
            itemMaterialGoodsBinding.ziyingIcon.setVisibility(View.GONE);
        }
        if (entity.getPromoteStatus() == 2&&!TextUtils.isEmpty(entity.getVipPrice())&&Double.valueOf(entity.getVipPrice())>0) {
            itemMaterialGoodsBinding.imgCx.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
            if (Double.valueOf(entity.getPromotePrice())<Double.valueOf(entity.getVipPrice())){
                itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPromotePrice()))));
                itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
            }else {
                itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getVipPrice()))));
                itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.iconVip.setVisibility(View.VISIBLE);
            }
        } else if (entity.getPromoteStatus()== 2){
            itemMaterialGoodsBinding.imgCx.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPromotePrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
            itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
        }else if (!TextUtils.isEmpty(entity.getVipPrice())&&Double.valueOf(entity.getVipPrice())>0){
            itemMaterialGoodsBinding.imgCx.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getVipPrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.iconVip.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
        } else {
            itemMaterialGoodsBinding.imgCx.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.valueOf(entity.getOriginPrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
        }
        if (LocationUtil.isOpenLocation(context) && LocationUtil.currentLocationIsValid()) {
            itemMaterialGoodsBinding.distanceView.setVisibility(View.VISIBLE);
        } else {
            itemMaterialGoodsBinding.distanceView.setVisibility(View.GONE);
        }
        itemMaterialGoodsBinding.tvStorageName.setText("仓储:" + entity.getStorageName());
    }

}
